<?php



$pattern = '\\[(\\[?)(embed|wp_caption|caption|gallery|audio|video|theme-my-login)(?![\\w-])([^\\]\\/]*(?:\\/(?!\\])[^\\]\\/]*)*?)(?:(\\/)\\]|\\](?:([^\\[]*+(?:\\[(?!\\/\\2\\])[^\\[]*+)*+)\\[\\/\\2\\])?)(\\]?)';
$data = '[gallery choice="1" ids="304,303,302,301,300"][gallery choice="2" ids="305,306,307,308,309"]';
//$data = '[gallery choice="1" ids="304,303,302,301,300"]';

preg_match_all('/' . $pattern . '/s', $data, $regex_matches);

print_r($regex_matches);
echo '<hr/>';
echo count($regex_matches[2]);
echo '<hr/>';
echo $regex_matches[2][0];
echo '<hr/>';
echo $regex_matches[2][1];
echo '<hr/>';
echo $regex_matches[3][0];
echo '<hr/>';
echo $regex_matches[3][1];
echo '<hr/>';

for ($i = 0; $i < count($regex_matches[2]); $i++) {
	if ($regex_matches[2][$i] == 'gallery') {
		echo 'true';
		echo '<hr/>';
		$attrs = str_replace(' ', '&', trim( $regex_matches[3][$i] ) );
		echo $attrs;
		echo '<hr/>';
		
		$attrs = str_replace('"', '', $attrs);
		echo $attrs;
		echo '<hr/>';
		
		/*
		$attr = wp_parse_args ($attrs, $defaults);
		$choice = $attr['choice'];
		echo $choice;
		echo '<hr/>';
		*/
	}
}
?>